package fsoft.com.vn.directory.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import fsoft.com.vn.directory.form.Address;
import fsoft.com.vn.directory.form.Organization;
import fsoft.com.vn.directory.form.Town;
import fsoft.com.vn.directory.service.OrganizationService;

@Controller
public class OrganizationController {

	@Autowired
	private OrganizationService organizationService;

	@RequestMapping(value = "/addorganization", method = RequestMethod.GET)
	public String addOrganizationGet(Map<String, Object> map,
			HttpServletRequest request) {
		map.put("organization", new Organization());
		map.put("townList", organizationService.loadOjbects("Town"));
		map.put("countyList", organizationService.loadOjbects("County"));
		map.put("countryList", organizationService.loadOjbects("Country"));
		return "addorganization";
	}

	@RequestMapping(value = "/addorganization", method = RequestMethod.POST)
	public ModelAndView addOrganization(Map<String, Object> map,
			@ModelAttribute("organization") @Valid Organization organization,
			BindingResult result, ModelAndView mav) {
		mav.setViewName("addorganization");
		if (result.hasErrors()) {
			map.put("townList", organizationService.loadOjbects("Town"));
			map.put("countyList", organizationService.loadOjbects("County"));
			map.put("countryList", organizationService.loadOjbects("Country"));
			return mav;
		} else {
			Address address = null;
			if (organization.getAddress().getId() > 0) {
				address = new Address();
				address.setId(organization.getAddress().getId());
				organization.setAddress(address);
				organizationService.addOrganization(organization, false);
			} else {
				address = new Address();
				address.setAddress1(organization.getAddress().getAddress1());
				address.setAddress3(organization.getAddress().getAddress3());
				address.setAddress2(organization.getAddress().getAddress2());
				address.setPostcode(organization.getAddress().getPostcode());

				Town town = new Town();
				town.setId(organization.getAddress().getTown().getId());
				address.setTown(town);

				organizationService.addOrganization(organization, true);
			}

			mav.setViewName("redirect:/organization");
			return mav;
		}
	}

	@RequestMapping(value = "/organization")
	public String listContacts(Map<String, Object> map, HttpServletRequest req) {
		map.put("organization", new Organization());
		map.put("organizationList", organizationService.listOrganization());
		return "organization";
	}
}